#include "curveproperties.h"

CurveProperties::CurveProperties( CurveType cType )
    : cType( cType )
{
    is_active              = true;

    mouseover_color        = QColor::fromRgbF( 1, 1, 1, 1 );
    point_color            = QColor::fromRgbF( 1, 0, 0, 1 );
    split_point_color      = QColor::fromRgbF( 1, 1, 0, 1 );
    active_point_color     = QColor::fromRgbF( 0, 1, 0, 1 );
    selected_point_color   = QColor::fromRgbF( 0, 0, 1, 1 );
    curve_color            = QColor::fromRgbF( 0, 0, 0, 1 );
    polyline_color         = QColor::fromRgbF( 1, 0.5, 0, 1 );
    convex_hull_color      = QColor::fromRgbF( 0, 0, 1, 1 );
    selection_color        = QColor::fromRgbF( 0.9, 0.9, 0.9, 1 );
    selection_border_color = QColor::fromRgbF( 0, 0, 0, 1 );

    curve_thickness = 2.0;
    point_radius    = 3.0;

    no_of_segments = 100;
    selected_point = -1;

    draw_curve             = true;
    draw_control_points    = true;
    draw_control_polyline  = false;
    draw_convex_hull       = true;
    draw_selection_box     = false;

    topLeft = bottomRight = QPointF( -1.0, -1.0 );
}

CurveProperties::~CurveProperties()
{
}

QColor CurveProperties::MouseOverColor( ) const
{
    return mouseover_color;
}

QColor CurveProperties::SelectedPointColor( ) const
{
    return selected_point_color;
}

QColor CurveProperties::PointColor( ) const
{
    if ( is_active )
        return active_point_color;
    else
        return point_color;
}

QColor CurveProperties::SplitPointColor( ) const
{
    return split_point_color;
}

void CurveProperties::SetCurveColor( QColor newColor )
{
    curve_color = newColor;
}

QColor CurveProperties::CurveColor( ) const
{
    return curve_color;
}

QColor CurveProperties::PolylineColor( ) const
{
    return polyline_color;
}

QColor CurveProperties::ConvexHullColor( ) const
{
    return convex_hull_color;
}

QColor CurveProperties::SelectionColor( ) const
{
    return selection_color;
}

QColor CurveProperties::SelectionBorderColor( ) const
{
    return selection_border_color;
}

void CurveProperties::SetCurveThickness( float newThickness )
{
    curve_thickness = newThickness;
}

float CurveProperties::CurveThickness( ) const
{
    return curve_thickness;
}

float CurveProperties::PointRadius( ) const
{
    return point_radius;
}

void CurveProperties::ClearBorders( )
{
    topLeft.setX( -1.0 );
    topLeft.setY( -1.0 );
    bottomRight.setX( -1.0 );
    bottomRight.setY( -1.0 );
}

void CurveProperties::ExtendBorders( QPointF point )
{
    if ( bottomRight.y( ) > point.y( ) || bottomRight.y( ) == -1.0 )
        bottomRight.setY( point.y( ) );

    if ( topLeft.y( ) < point.y( ) || topLeft.y( ) == -1.0 )
        topLeft.setY( point.y( ) );

    if ( topLeft.x( ) > point.x( ) || topLeft.x( ) == -1.0 )
            topLeft.setX( point.x( ) );

    if ( bottomRight.x( ) < point.x( ) || bottomRight.x( ) == -1.0 )
        bottomRight.setX( point.x( ) );
}

void CurveProperties::SetBorders( vector< QPointF > points )
{
    if ( points.size( ) < 1 )
    {
        topLeft.setX( -1.0 );
        topLeft.setY( -1.0 );
        bottomRight.setX( -1.0 );
        bottomRight.setY( -1.0 );

        return;
    }

    double left, right, top, bottom;
    vector< QPointF >::iterator it = points.begin( );

    left   = (*it).x( );
    right  = (*it).x( );
    top    = (*it).y( );
    bottom = (*it).y( );

    for ( it = points.begin( ) + 1; it != points.end( ); ++it )
    {
	if ( (*it).y( ) > top )
	    top = (*it).y( );
	else if ( (*it).y( ) < bottom )
	    bottom = (*it).y( );

	if ( (*it).x( ) < left )
	    left = (*it).x( );
	else if ( (*it).x( ) > right )
	    right = (*it).x( );
    }

    SetBorders( left, right, top, bottom );

}

void CurveProperties::SetBorders( double left, double right,
				  double top,  double bottom )
{
    topLeft.setX( left );
    topLeft.setY( top );
    bottomRight.setX( right );
    bottomRight.setY( bottom );
}

QPointF* CurveProperties::Borders( ) const
{
    QPointF* points = new QPointF[2];

    points[0] = topLeft;
    points[1] = bottomRight;

    return points;
}

void CurveProperties::SetSelectedPoint( int selected_point_index )
{
    selected_point = selected_point_index;
}

int CurveProperties::SelectedPoint( ) const
{
    return selected_point;
}

void CurveProperties::SetNumberOfSegments( int num )
{
    no_of_segments = num;
}

int CurveProperties::NumberOfSegments( ) const
{
    return no_of_segments;
}

void CurveProperties::ActivateCurve( )
{
    is_active          = true;
    draw_selection_box = true;
}

void CurveProperties::DeactivateCurve( )
{
    is_active          = false;
    draw_selection_box = false;
}

void CurveProperties::ShowCurve( )
{
    draw_curve = true;
}

void CurveProperties::ShowCurve( bool state )
{
    draw_curve = state;
}

void CurveProperties::HideCurve( )
{
    draw_curve = false;
}

void CurveProperties::ShowControlPoints( )
{
    draw_control_points = true;
}

void CurveProperties::ShowControlPoints( bool state )
{
    draw_control_points = state;
}

void CurveProperties::HideControlPoints( )
{
    draw_control_points = false;
}

void CurveProperties::ShowControlPolyline( )
{
    draw_control_polyline = true;
}

void CurveProperties::ShowControlPolyline( bool state )
{
    draw_control_polyline = state;
}

void CurveProperties::HideControlPolyline( )
{
    draw_control_polyline = false;
}

void CurveProperties::ShowConvexHull( )
{
    draw_convex_hull = true;
}

void CurveProperties::ShowConvexHull( bool state )
{
    draw_convex_hull = state;
}

void CurveProperties::HideConvexHull( )
{
    draw_convex_hull = false;
}

void CurveProperties::ShowSelectionBox()
{
    draw_selection_box = true;
}

void CurveProperties::ShowSelectionBox( bool state )
{
    draw_selection_box = state;
}

void CurveProperties::HideSelectionBox()
{
    draw_selection_box = false;
}

bool CurveProperties::DrawCurve( ) const
{
    return draw_curve;
}

bool CurveProperties::DrawControlPoints( ) const
{
    return draw_control_points;
}

bool CurveProperties::DrawControlPolyline( ) const
{
    return draw_control_polyline;
}

bool CurveProperties::DrawConvexHull( ) const
{
    return draw_convex_hull;
}

bool CurveProperties::DrawSelectionBox( ) const
{
    return draw_selection_box;
}

CurveType CurveProperties::Type( )
{
    return cType;
}
